package com.gcd;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gcd.R;
import com.gcd.knowledge.PARAMETERS;


public class EditKonstanta extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_konstanta);
		
		// Get Parameter Teks from EditKnowledge
		Bundle bundle = this.getIntent().getExtras();
		String Teks = bundle.getString("Teks");
		float Value = bundle.getFloat("Value");
		
		
		// Get Parameter Teks View from edit_konstanta layout
		TextView Judul = (TextView) findViewById(R.id.textView1);
		Judul.setText(Teks);
		
		// Get Parameter EditTeks from edit_konstanta layout
		EditText Nilai = (EditText) findViewById(R.id.editText1);
		Nilai.setText(Float.toString(Value));
	}
	
	public void btnClick(View v){
		 EditText Value = (EditText) findViewById(R.id.editText1);
		 float floatValue;
		 
		 Bundle bundle = this.getIntent().getExtras();
		 int Id = bundle.getInt("Id"); 
		 
		 floatValue = Float.parseFloat(Value.getText().toString());
		 updateDB(floatValue, Id);
	}
	
	private void updateDB(float v, int id){
		
		DatabaseHelper databaseHelper = new DatabaseHelper(this);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		
		ContentValues updateFloat = new ContentValues();
		updateFloat.put("nilai", v);
		db.update("relasi", updateFloat, "id=?", new String[]{Integer.toString(id)});
		
		// Change Parameter with new Value
		ChangeParameter(id, v);
		System.out.println(PARAMETERS.LAKI_LAKI);
		
		AlertDialog popUp = new android.app.AlertDialog.Builder(this).create();
		popUp.setTitle("Update Berhasil");
		popUp.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		popUp.show();

		db.close();	
	}
	
	private void ChangeParameter(int id, float newValue){
		if(id == 1){
			PARAMETERS.PEREMPUAN_SATU = newValue;
		} else if(id == 2){
			PARAMETERS.PEREMPUAN_N = newValue;
		} else if(id == 3){
			PARAMETERS.LAKI_LAKI = newValue;
		} else if(id == 4){
			PARAMETERS.PEREMPUAN = newValue;
		} else if(id == 5){
			PARAMETERS.ISTRI_HAS_ANAK_YA = newValue;
		}else if(id == 6){
			PARAMETERS.ISTRI_HAS_ANAK_TIDAK = newValue;
		}else if(id == 7){
			PARAMETERS.SUAMI_HAS_ANAK_YA = newValue;
		}else if(id == 8){
			PARAMETERS.SUAMI_HAS_ANAK_TIDAK = newValue;
		} else if(id == 9){
			PARAMETERS.AYAH_HAS_IBU_HAS = newValue;
		} else if(id == 10){
			PARAMETERS.AYAH_HAS_IBU_HASNOT = newValue;
		} else if(id == 11){
			PARAMETERS.AYAH_HASNOT_IBU_HAS = newValue;
		} else if(id == 12){
			PARAMETERS.AYAH_HAS_IBU_HAS_SAUDARA_HAS = newValue;
		}else if(id == 13){
			PARAMETERS.AYAH_HASNOT_IBU_HAS_SAUDARA_HAS = newValue;
		}else if(id == 14){
			PARAMETERS.AYAH_HAS_IBU_HAS_SAUDARA_HASNOT = newValue;
		}else if(id == 15){
			PARAMETERS.AYAH_HASNOT_IBU_HAS_SAUDARA_HASNOT = newValue;
		}
	}
}
